package Client;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MouseEvent;
import org.w3c.dom.views.AbstractView;

/**
 * CardComponent extends JLabel and add some feature
 * @author Termchai 5510546042, Eknarin 5510546239
 *
 */
public class CardComponent extends JLabel {

	public static int count;
	private static int[] s1,s2 = null;
	private Card card;
	private int select; // 0 >> non-select, 1 >> selected, 2 >> FaceUp
	private GUI gui;
	private Timer timer;
	private Turn player;
	private Client client;
	private MesSelect mesSelect;
	private ClickAction clickAction;
	private GameConsole gameConsole;
	private static int tempTurn;
	private ImageIcon imgCard;
	
	/**
	 * 
	 * @param name
	 * @param card
	 * @param gui
	 * @param client
	 * @param gameConsole
	 */
	public CardComponent(String name, Card card, GUI gui, Client client, GameConsole gameConsole) {
		super(name);
		this.gui=gui;
		this.card = card;
		this.setOpaque(true);
		imgCard = new ImageIcon(this.getClass().getResource("/images/card.jpg"));
		this.setIcon(imgCard);
		this.setBackground(Color.blue);
		this.player=gui.getPlayer();
		this.client=client;
		this.gameConsole=gameConsole;
		
		tempTurn=1;
		Timer timer = new Timer(2000, new FaceUpAction(gui));
		timer.setRepeats(false);
		clickAction = new ClickAction(this, gui, timer,true);
		this.addMouseListener(new ClickAction(this,gui,timer,false));
	}
	
	
	public ClickAction getClickAction()
	{
		return clickAction;
	}
	
	public Card getCard()
	{
		return card;
	}
	
	/**
	 * 
	 * @return return card selected
	 */
	public int getSelect() {
		return select;
	}

	/**
	 * clear selected
	 */
	public static void clearSelect()
	{
		s1=null;
		s2=null;
		count=0;
	}
	
	/**
	 * set selecet of card.
	 * 0 = facedown
	 * 1 = faceup
	 * 2 = matching
	 * @param x 0,1,2
	 */
	public void setSelect(int x) {
		select = x;
		if (x==0)
		{
			this.setIcon(imgCard);
		}
		else if (x==1)
		{
			this.setIcon(card.getValue());
			this.setBackground(Color.red);
		}
		else if (x==2)
		{
			this.setIcon(card.getValue());
			this.setBackground(Color.red);
		}
		
	}
	
	/**
	 * 
	 * @param x index of card selected
	 * @return card selected
	 */
	public static int[] getCardSelect(int x)
	{
		if (x==1)
			return s1;
		else if (x==2)
			return s2;
		else
			return null;
	}

	/**
	 * function when click on CardComponent
	 * @author Termchai 5510546042, Eknarin 5510546239
	 *
	 */
	class ClickAction implements MouseListener {
		private CardComponent cc;
		private GUI gui;
		private GameConsole gameConsole;
		private Timer timer;
		private boolean fromServer;
		
		public ClickAction(CardComponent cc,GUI gui,Timer timer,boolean fromServer) {
			this.cc = cc;
			this.gui=gui;
			this.gameConsole=gui.getGameConsole();
			this.timer=timer;
			this.fromServer=fromServer;
			
		}
		
		@Override
		public void mouseClicked(java.awt.event.MouseEvent e) {

			if (fromServer ||gameConsole.getTurn() == player && cc.getSelect()==0)
			{
				
			// TODO Auto-generated method stub
			if (cc.getSelect()==0)
			{
				if (count<2)
				{
					cc.setSelect(1);
					count++;
					System.out.println(" "+gameConsole.getTurn()+"----------------");
					System.out.println("Pre "+gameConsole.getPreTurn()+"----------------");
					mesSelect = new MesSelect(card.getLocation());
					System.out.println(" "+gameConsole.getTurn()+"----------------");
					System.out.println("Pre "+gameConsole.getPreTurn()+"----------------");
					try {
						client.sendToServer(mesSelect);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					if (s1==null)
					{
						s1=cc.getCard().getLocation();
					}
					else if (s2==null)
					{
						s2=cc.getCard().getLocation();
					}
				}
			}
			/*
			else if (cc.getSelect()==1)
			{
				cc.setSelect(0);
				count--;

				if (s1!=null)
				if (s1[0]==cc.getCard().getLocation()[0] && s1[1]==cc.getCard().getLocation()[1])
				{
					s1=null;
				}
				
				if (s2!=null)
				if (s2[0]==cc.getCard().getLocation()[0] && s2[1]==cc.getCard().getLocation()[1])
				{
					System.out.println("s2 = null");
					s2=null;
				}	
			}
			*/
			
			if (CardComponent.getCardSelect(1)!=null && CardComponent.getCardSelect(2)!=null)
			{
				
				System.out.println("Card 1 : " + gui.getCard(CardComponent.getCardSelect(1)).getValue());
				System.out.println("Card 2 : " + gui.getCard(CardComponent.getCardSelect(2)).getValue());
				if (gui.getCard(CardComponent.getCardSelect(1)).getValue() == gui.getCard(CardComponent.getCardSelect(2)).getValue())
				{
					System.out.println("getTurn before check" + gameConsole.getTurn());
					if (gameConsole.getTurn()==Turn.P1 && gui.getGameConsole().getInfo().getScore()[0]+gui.getGameConsole().getInfo().getScore()[1]!=15)
					{
						gui.plusScore(0);
					}
					else if (gameConsole.getTurn()==Turn.P2 && gui.getGameConsole().getInfo().getScore()[0]+gui.getGameConsole().getInfo().getScore()[1]!=15)
					{
						gui.plusScore(1);
					}
					gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(2);
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(2);
					gui.clear();

					gui.updateTurn();
				}
				else 
				{

					System.out.println(" "+gameConsole.getTurn()+"----------------");
					System.out.println("Pre "+gameConsole.getPreTurn()+"----------------");
					
					gameConsole.setPreTurn(gameConsole.getTurn());
					System.out.println(" "+gameConsole.getTurn()+"----------------");
					System.out.println("Pre "+gameConsole.getPreTurn()+"----------------");
					if (gameConsole.getTurn()==Turn.P1)
					{
						gameConsole.setTurn(Turn.P2);
						gameConsole.setPreTurn(Turn.P2);
					}
					else if (gameConsole.getTurn()==Turn.P2)
					{
						gameConsole.setTurn(Turn.P1);
						gameConsole.setPreTurn(Turn.P1);
					}
					gameConsole.setTurn(Turn.Wait);

					System.out.println(gameConsole.getPreTurn()+"----------------");
					gui.updateTurn();
					
					gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(2);
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(2);
					
						timer.start();
				}
			}
			
			System.out.println(cc.getCard()+"");
			System.out.println(s1+"");
			System.out.println(s2+"");
			}
		}

		@Override
		public void mouseEntered(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

	}
	
	/**
	 * sub class for use in ClickAction
	 * @author Termchai 5510546042, Eknarin 5510546239
	 *
	 */
	class FaceUpAction implements ActionListener
	{
		private GUI gui;

		public FaceUpAction(GUI gui)
		{
			this.gui=gui;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if (gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]]!=null &&
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]]!=null)
			{
			gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(0);
			gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(0);
			
			}
			gui.clear();
			System.out.println("gameConsole :: "+gameConsole.getTurn());
			System.out.println("tempTurn :: "+tempTurn);
			if(tempTurn==1)
			{
				gameConsole.setTurn(Turn.P2);
				tempTurn=2;
			}
			else if (tempTurn==2)
			{
				gameConsole.setTurn(Turn.P1);
				tempTurn=1;
			}
			System.out.println("gameConsole :: "+gameConsole.getTurn());
			System.out.println("tempTurn :: "+tempTurn);
			gui.updateTurn();
			
		}
		
	}
}
